import vnode from "./vnode";

export default function (sel, data, children) {
  if (arguments.length !== 3) {
    throw new Error("h函数的参数必须为3个");
  }
  /* {key, sel, data, children, text, elm } */
  if (["object", "string"].includes(typeof children) && children !== null) {
    // 是数组或者对象
    if (Array.isArray(children)) {
      // 是数组
      const newChild = children.map((item) => {
        if (typeof item === "string") {
          return vnode(undefined, undefined, undefined, undefined, item);
        }
        return item;
      });
      return vnode(data?.key, sel, data, newChild);
    } else if (typeof children === "string") {
      return vnode(data?.key, sel, data, undefined, children);
    } else {
      // 是对象
      return vnode(data?.key, sel, data, children);
    }
  } else {
    throw new Error("第三个参数不合规");
  }
}
